import { ref, computed } from "vue";
import { defineStore } from "pinia";
import { get } from "@/api/http";

import { dictionarys } from "./dictionary";
interface Option {
  value: string | number;
  label: string;
  color?: string;
  prop?: string;
  duty?: string;
}
export interface Dictionary {
  [key: string]: Option[]; //规定对象属性为key，以免在proform组件中警告
  stStatus: Option[];
  gender: Option[];
  position: Option[];
}

export default defineStore("global", () => {
  const dictionary = ref(dictionarys);

  /**
   * 根据字典值value查找label
   * @param key 数据字段名称
   * @param source 行数据对象
   */
  const getValByDictionary = (key: string, source: any) => {
    // console.log(key);
    const currentList = dictionary.value[key];
    const value = source[key];
    const option = currentList.filter((o) => o.value == value);
    if (!option[0]) {
      console.warn(`字段为${key}的数据回显错误,请排查是否为脏数据`);
      return "";
    }
    return option[0].label;
  };

  // 人员列表
  const staffList = ref<any[]>([]);
  async function updateStaffList() {
    const list = await get("/watch/staff/list");
    const option = list.map((p: any) => ({
      value: p.id,
      label: p.name,
    }));
    staffList.value = option;
    dictionary.value.staffId = option;
  }

  return { dictionary, getValByDictionary, staffList, updateStaffList };
});
